##=================================================================================================##
## Replication File for Appendix A1 												                                       ##	
## "Resist to Commit: Concrete Campaign Commitments and the Need to Clarify a Partisan Reputation" ##
## Journal of Politics, Forthcoming.															                                 ##
## Authors: Eichorst and Lin 																		                                   ##
## Date: 2017.11.22																				                                         ##
##=================================================================================================##
  

rm(list = ls())
library(foreign)
library(ggplot2)
library(readstata13)
library(gplots)
library(bootstrap)

data.pair <- read.dta13("EL_JOP_AppendixA1.dta")
data.cate <- read.dta13("EL_JOP_AppendixA2.dta")

### Create a flip-a-coin exercise

data.pair$simulation <- rep(NA,255)

set.seed(201703)
for (i in 1:length(data.pair$simulation)) {
    
  n = 8 ## flip the coin 8 times
  a = sample(c("1", "0"), n, rep = T)
  data.pair$simulation[i] <- sum(a==1)  ## sum how many heads (concrete sentence) we have.
  
}  


### Test the above two distributions
t.test(data.pair$numCorrect, data.pair$simulation) 

t.test(data.cate$numCorrect, data.pair$simulation) 

t.test(data.cate$numCorrect, data.pair$numCorrect) 

### Figure A1(a): Plot these two distributions (PairwiseComparison)

x = data.pair$numCorrect
y = data.pair$simulation

pdf("Fig_DistPair.pdf", width = 7.5, height = 6)
hist(data.pair$numCorrect, 
     xlim = c(0, max(x)), 
     probability = TRUE, 
     nclass = max(x) - min(x) + 1, 
     col = 'lightgrey',ylab=NULL,xlab=NULL,main=NULL)
lines(density(x, bw=1), col = 'navy', lty = 1, lwd = 2)
lines(density(y, bw=1), col = 'red',lty = 2, lwd = 2)
leg.txt <- c("Survey Sample", "Coin Flip")
legend(list(x=0 ,y=0.26), legend = leg.txt, lty = c(1,2), col = c("navy","red"), cex=1.2, bty="n")
mtext("Number of Correctly Answered Pairs",side=1, las=1, line=2.25,font=1.2,cex=1.2)
mtext("Density",side=2,line=2.55,font=1.2, las=3 ,cex=1.2)
mtext("Distribution of Survey Respondents (Pairwise Comparison)",side=3,line=1, las =1, font=1.5,cex=1.2)
dev.off()


### Figure A1(b): Plot the Difference in Means using Bootstrap 
b1 <- bootstrap(x, 5000, mean)
b2 <- bootstrap(y, 5000, mean)
b_diff <- b1$thetastar - b2$thetastar
b_diff <- as.numeric(b_diff) 


pdf("Fig_BootDiffPair.pdf", width = 7.5, height = 6)
hist(b_diff, 
     xlim = c(1.4, max(b_diff)), 
     probability = TRUE, 
     nclass = max(b_diff) - min(b_diff) + 50, 
     col = 'lightgrey', ylab=NULL, xlab=NULL, main=NULL, xaxt="n")
axis(1, las=1)
abline(v=mean(b_diff), lty=2, lwd=3, col="navy")
text(1.82, 3.18, "Mean = 1.92",cex = 1.2, col = 'navy')
mtext("Difference in Means (Pairwise Comparison)",side=1, las=1, line=2.25,font=1.2,cex=1.2)
mtext("Density",side=2, las=3, line=2.45,font=1.2,cex=1.2)
mtext("Bootstrapped Difference in Means (Survey Sample vs. Coin Flip)",side=3, las=1, line=.5, font=1.5,cex=1.2)
dev.off()



### Figure A2(a): Plot these two distributions (Categorization)

z = data.cate$numCorrect
y = data.pair$simulation

pdf("Fig_DistCategorization.pdf", width = 7.5, height = 6)
hist(data.cate$numCorrect, 
     xlim = c(0, max(z)), 
     probability = TRUE, 
     nclass = max(z) - min(z) + 1, 
     col = 'lightgrey',ylab=NULL,xlab=NULL,main=NULL)
lines(density(z, bw=1), col = 'navy', lty = 1, lwd = 2)
lines(density(y, bw=1), col = 'red',lty = 2, lwd = 2)
leg.txt <- c("Survey Sample", "Coin Flip")
legend(list(x=0 ,y=0.26), legend = leg.txt, lty = c(1,2), col = c("navy","red"), cex=1.2, bty="n")
mtext("Number of Correctly Categorized Sentence",side=1, las=1, line=2.25,font=1.2,cex=1.2)
mtext("Density",side=2,line=2.55,font=1.2, las=3 ,cex=1.2)
mtext("Distribution of Survey Respondents (Categorization)",side=3,line=1, las =1, font=1.5,cex=1.2)
dev.off()


### Figure A2(b): Plot the Difference in Means using Bootstrap 
b1 <- bootstrap(z, 5000, mean)
b2 <- bootstrap(y, 5000, mean)
b_diff <- b1$thetastar - b2$thetastar
b_diff <- as.numeric(b_diff) 


pdf("Fig_BootDiffCategory.pdf", width = 7.5, height = 6)
hist(b_diff, 
     xlim = c(1, max(b_diff)), 
     probability = TRUE, 
     nclass = max(b_diff) - min(b_diff) + 50, 
     col = 'lightgrey', ylab=NULL, xlab=NULL, main=NULL, xaxt="n")
axis(1, las=1)
abline(v=mean(b_diff), lty=2, lwd=3, col="navy")
text(1.59, 3.44, "Mean = 1.49",cex = 1.2, col = 'navy')
mtext("Difference in Means (Sentence Categorization)",side=1, las=1, line=2.25,font=1.2,cex=1.2)
mtext("Density",side=2, las=3, line=2.45,font=1.2,cex=1.2)
mtext("Bootstrapped Difference in Means (Survey Sample vs. Coin Flip)",side=3, las=1, line=.5, font=1.5,cex=1.2)
dev.off()




